package com.ruoyi.spicfz.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.DictService;
import com.ruoyi.spicfz.domain.Executives;
import com.ruoyi.spicfz.domain.ExecutivesCompos;
import com.ruoyi.spicfz.mapper.ExecutivesMapper;
import com.ruoyi.spicfz.service.IExecutivesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 高管信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-02
 */
@Service
public class ExecutivesServiceImpl implements IExecutivesService 
{
    @Autowired
    private ExecutivesMapper executivesMapper;

    @Autowired
    private DictService dictService;
    /**
     * 查询高管信息
     * 
     * @param executiveId 高管信息ID
     * @return 高管信息
     */
    @Override
    public Executives selectExecutivesById(Long executiveId)
    {
        return executivesMapper.selectExecutivesById(executiveId);
    }

    /**
     * 查询高管信息列表
     * 
     * @param executives 高管信息
     * @return 高管信息
     */
    @Override
    public List<Executives> selectExecutivesList(Executives executives)
    {
        return executivesMapper.selectExecutivesList(executives);
    }

    /**
     * 新增高管信息
     * 
     * @param executives 高管信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertExecutives(Executives executives)
    {
        executives.setCreateTime(DateUtils.getNowDate());
        int rows = executivesMapper.insertExecutives(executives);
        insertExecutivesCompos(executives);
        return rows;
    }

    /**
     * 修改高管信息
     * 
     * @param executives 高管信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateExecutives(Executives executives)
    {
        executives.setUpdateTime(DateUtils.getNowDate());
        executivesMapper.deleteExecutivesComposByExecutiveId(executives.getExecutiveId());
        insertExecutivesCompos(executives);
        return executivesMapper.updateExecutives(executives);
    }

    /**
     * 删除高管信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteExecutivesByIds(String ids)
    {
        executivesMapper.deleteExecutivesComposByExecutiveIds(Convert.toStrArray(ids));
        return executivesMapper.deleteExecutivesByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除高管信息信息
     * 
     * @param executiveId 高管信息ID
     * @return 结果
     */
    @Override
    public int deleteExecutivesById(Long executiveId)
    {
        executivesMapper.deleteExecutivesComposByExecutiveId(executiveId);
        return executivesMapper.deleteExecutivesById(executiveId);
    }

    /**
     * 新增高管任职信息
     * 
     * @param executives 高管信息对象
     */
    public void insertExecutivesCompos(Executives executives)
    {
        List<ExecutivesCompos> executivesComposList = executives.getExecutivesComposList();
        Long executiveId = executives.getExecutiveId();
        if (StringUtils.isNotNull(executivesComposList))
        {
            List<ExecutivesCompos> list = new ArrayList<ExecutivesCompos>();
            for (ExecutivesCompos executivesCompos : executivesComposList)
            {
                executivesCompos.setExecutiveId(executiveId);
                //增加高管姓名、公司名称、职位名称
                executivesCompos.setExecutiveName(executives.getExecutiveName());
                executivesCompos.setCompanyName(dictService.getLabel("spicfz_company", executivesCompos.getCompanyId().toString()));
                executivesCompos.setPositionName(dictService.getLabel("spicfz_position_type", executivesCompos.getPositionId()));
                list.add(executivesCompos);
            }
            if (list.size() > 0)
            {
                executivesMapper.batchExecutivesCompos(list);
            }
        }
    }
}
